package top.younote.cherry.selenium.impl;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.younote.cherry.selenium.DadaService;

import java.net.URL;
import java.util.Set;

/**
 * @Description:
 * @Author: duanluyao
 * @CreateTime: 2022/09/15 09:41
 */
@Slf4j
@Service
public class DadaServiceImpl implements DadaService {

    @Override
    public Cookie getDadaCookies(String username, String password) {
        try {
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.setHeadless(true);
            RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), chromeOptions);
            driver.get("https://dauth.corp.imdada.cn/sso?redirectUrl=http://123.corp.imdada.cn/app/#/");
            driver.findElementById("accountNameForPwd").sendKeys(username);
            driver.findElementById("user-password").sendKeys(password);
            driver.findElementById("pwd-login").click();
            Set<Cookie> cookies = driver.manage().getCookies();
            return cookies.iterator().next();
        } catch (Exception e) {
            log.error("获取 Cookies 失败！", e);
        }
        return null;
    }

    @Override
    public void getHoliday(Cookie cookie) {
//        RestTemplate restTemplate = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
//        headers.put(HttpHeaders.COOKIE,cookie.toString());
//        ResponseEntity<String> result = restTemplate.getForEntity("http://123.corp.imdada.cn/api/v1/user/profile/info", String.class);
//        System.out.println(result);
    }
}
